package com.ruoyi.SSL.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyun.cdn20180510.models.DescribeDomainCertificateInfoResponseBody;
import com.aliyun.cdn20180510.models.DescribeUserDomainsResponseBody;
import com.aliyun.oss.model.Bucket;
import com.qiniu.util.Json;
import com.ruoyi.SSL.domain.SslApply;
import com.ruoyi.SSL.domain.SslCloudProductDeploy;
import com.ruoyi.SSL.domain.SslDomain;
import com.ruoyi.SSL.service.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.utils.aly.AlyCDNDomainApiUtils;
import com.ruoyi.utils.aly.AlyDCDNDomainApiUtils;
import com.ruoyi.utils.aly.AlyOssDomianApiUtils;
import com.ruoyi.utils.qny.QnyCdnDomainUtils;
import com.ruoyi.utils.qny.QnyDcdnDomainUtils;
import com.ruoyi.utils.qny.QnySslUtils;
import com.ruoyi.utils.txy.TxyCdnDomainUtils;
import com.ruoyi.utils.txy.TxyEdgeOneDomainUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AutoDeployServiceImpl extends BaseController implements IAutoDeployService {

    @Autowired
    private ISslCloudProductDeployService cloudProductDeployService;

    @Autowired
    private ISslDomainService domainService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISslApplyService applyService;

    @Autowired
    private ISslCloudKeyService cloudKeyService;


    @Override
    public AjaxResult getDomainByCsAndKeySercet(String cs, String cloudProduct, String accessKeyId, String accessSecret) {
        AjaxResult aj = new AjaxResult();
        //判断厂商
        if(StrUtil.equals(cs, "aly")){
            //判断是什么产品
            if(StrUtil.equals(cloudProduct, "aly-cdn")){
                aj = AlyCDNDomainApiUtils.selectCDNDmoainList(accessKeyId, accessSecret);
            }else if(StrUtil.equals(cloudProduct, "aly-dcdn")) {
                aj = AlyDCDNDomainApiUtils.selectDCDNDmoainList(accessKeyId, accessSecret, "");
            }else if(StrUtil.equals(cloudProduct, "aly-oss")){
                aj = AlyOssDomianApiUtils.selectBuchetsCNameList(accessKeyId, accessSecret);
            }else{
                aj = AjaxResult.error("暂不支持阿里云的【"+cloudProduct+"】产品！");
            }
        }else if(StrUtil.equals(cs, "qny")){
            //判断是什么产品
            if(StrUtil.equals(cloudProduct, "qny-cdn")){
                aj = QnyCdnDomainUtils.listCdnDomain(accessKeyId, accessSecret, "web");
            }else if(StrUtil.equals(cloudProduct, "qny-dcdn")) {
                aj = QnyDcdnDomainUtils.listDcdnDomain(accessKeyId, accessSecret);
            }else if(StrUtil.equals(cloudProduct, "qny-oss")){
                aj = QnyCdnDomainUtils.listCdnDomain(accessKeyId, accessSecret, "download");
            }else{
                aj = AjaxResult.error("暂不支持七牛云的【"+cloudProduct+"】产品！");
            }
        }else if(StrUtil.equals(cs, "txy")){
            //判断是什么产品
            if(StrUtil.equals(cloudProduct, "txy-cdn")){
                aj = TxyCdnDomainUtils.listCdnDomain(accessKeyId, accessSecret, "web");
            }else if(StrUtil.equals(cloudProduct, "txy-edgeOne")) {
                aj = TxyEdgeOneDomainUtils.listDescribeAccelerationDomains(accessKeyId, accessSecret);
            }else if(StrUtil.equals(cloudProduct, "txy-oss")){
                aj = TxyCdnDomainUtils.listCdnDomain(accessKeyId, accessSecret, "download");
            }else{
                aj = AjaxResult.error("暂不支持腾讯云的【"+cloudProduct+"】产品！");
            }
        }else{
            aj = AjaxResult.error("暂不支持【"+cs+"】厂商！");
        }
        return aj;
    }

    public AjaxResult addCloudProductDeploy(SslCloudProductDeploy cloudProductDeploy){
        AjaxResult aj = new AjaxResult();
        //判断添加什么产品
        SslCloudProductDeploy cloudProductDeployTmp = new SslCloudProductDeploy();
        cloudProductDeployTmp.setCertificateDomain(cloudProductDeploy.getCertificateDomain());
        List<SslCloudProductDeploy> deployList = cloudProductDeployService.selectSslCloudProductDeployList(cloudProductDeployTmp);
        if(deployList != null && deployList.size() > 0){
            return AjaxResult.error("该域名已存在部署任务了，请检查！");
        }
        cloudProductDeploy.setIssuingAuthority(configService.selectConfigByKey("default.issuing.authority"));
        cloudProductDeploy.setDeployStatus("N");
        //判断是否插入域名信息
        SslDomain sslDomain = new SslDomain();
        sslDomain.setCertificateDomain(cloudProductDeploy.getDomainTxt());
        List<SslDomain> domainList = domainService.selectSslDomainList(sslDomain);
        if(domainList == null || domainList.size() < 1){
            //插入域名数据
            sslDomain = new SslDomain();
            sslDomain.setPrivateKeyType("RSA");
            sslDomain.setCertificateDomain(cloudProductDeploy.getDomainTxt());
            sslDomain.setIsUniversal(cloudProductDeploy.getIsUniversal());
            sslDomain.setIsYzOver(cloudProductDeploy.getIsYzOver());
            sslDomain.setIssuingAuthority(cloudProductDeploy.getIssuingAuthority());
            sslDomain.setAutoExpiredTime(cloudProductDeploy.getAutoExpiredTime());
            sslDomain.setIsAutoSsl("N");
            sslDomain.setDeployStatus("N");
            sslDomain.setStatus("0");
            //插入域名数据
            domainService.insertSslDomain(sslDomain);
            //申请证书
            domainService.applySSL(sslDomain);
        }else{
            sslDomain = domainList.get(0);
            if(StrUtil.isBlankIfStr(sslDomain.getSslApplyId())){
                //申请证书
                return domainService.applySSL(sslDomain);
            }
        }
        return toAjax(cloudProductDeployService.insertSslCloudProductDeploy(cloudProductDeploy)).put("msg", "新增部署任务成功");
    }

    public AjaxResult deploySsl(String id){
        AjaxResult aj = new AjaxResult();
        //获取到自动部署任务
        SslCloudProductDeploy cloudProductDeploy = cloudProductDeployService.selectSslCloudProductDeployById(id);
        //得到域名信息
        SslDomain sslDomain = new SslDomain();
        sslDomain.setCertificateDomain(cloudProductDeploy.getDomainTxt());
        List<SslDomain> domainList = domainService.selectSslDomainList(sslDomain);
        sslDomain = domainList.get(0);
        //在得到当前域名的证书信息
        SslApply apply = applyService.selectSslApplyById(sslDomain.getSslApplyId());
        if(cloudProductDeploy != null && StrUtil.isNotBlank(cloudProductDeploy.getId())){
            //判断厂商
            if(cloudProductDeploy.getCloudVendor().equals("aly")){
                //判断产品
                if(cloudProductDeploy.getCloudProduct().contains("dcdn")){
                    aj = AlyDCDNDomainApiUtils.setDCDNDomainCSR(cloudProductDeploy.getAccessKeyId(), cloudProductDeploy.getAccessKeySecret(), apply, cloudProductDeploy.getCertificateDomain(), configService.selectConfigByKey("system.name"));
                }else if(cloudProductDeploy.getCloudProduct().contains("cdn")){
                    aj = AlyCDNDomainApiUtils.setCDNDomainCSR(cloudProductDeploy.getAccessKeyId(), cloudProductDeploy.getAccessKeySecret(), apply, cloudProductDeploy.getCertificateDomain(), configService.selectConfigByKey("system.name"));
                }else if(cloudProductDeploy.getCloudProduct().contains("oss")){
                    JSONObject rootJsonObj = JSONUtil.parseObj(cloudProductDeploy.getOtherJson());
                    JSONObject cloudDomainInfo = JSONUtil.parseObj(rootJsonObj.getStr("cloudDomainInfo"));
                    Bucket bucket = JSONUtil.toBean(cloudDomainInfo.getStr("bucket"), Bucket.class);
                    aj = AlyOssDomianApiUtils.putCnameSSL(cloudProductDeploy.getAccessKeyId(), cloudProductDeploy.getAccessKeySecret(),apply, bucket, cloudProductDeploy.getCertificateDomain());
                }else{
                    aj = AjaxResult.error("未知【"+cloudProductDeploy.getCloudVendor()+"】云产品！");
                }
            }else if(cloudProductDeploy.getCloudVendor().equals("qny")){
                //判断产品
                if(cloudProductDeploy.getCloudProduct().contains("dcdn")){
                    aj = QnyDcdnDomainUtils.putDCdnDomainHttps(cloudProductDeploy.getAccessKeyId(), cloudProductDeploy.getAccessKeySecret(), apply, cloudProductDeploy.getCertificateDomain(), configService.selectConfigByKey("system.name"));
                }else if(cloudProductDeploy.getCloudProduct().contains("cdn")){
                    aj = QnyCdnDomainUtils.putCdnDomainHttps(cloudProductDeploy.getAccessKeyId(), cloudProductDeploy.getAccessKeySecret(), apply, cloudProductDeploy.getCertificateDomain(), configService.selectConfigByKey("system.name"));
                }else if(cloudProductDeploy.getCloudProduct().contains("oss")){
                    aj = QnyCdnDomainUtils.putCdnDomainHttps(cloudProductDeploy.getAccessKeyId(), cloudProductDeploy.getAccessKeySecret(), apply, cloudProductDeploy.getCertificateDomain(), configService.selectConfigByKey("system.name"));
                }else{
                    aj = AjaxResult.error("未知【"+cloudProductDeploy.getCloudVendor()+"】云产品！");
                }
            }else if(cloudProductDeploy.getCloudVendor().equals("txy")){
                //判断产品
                if(cloudProductDeploy.getCloudProduct().contains("txy-edgeOne")){
                    JSONObject rootJsonObj = JSONUtil.parseObj(cloudProductDeploy.getOtherJson());
                    JSONObject cloudDomainInfo = JSONUtil.parseObj(rootJsonObj.getStr("cloudDomainInfo"));
                    aj = TxyEdgeOneDomainUtils.putDescribeAccelerationDomainCert(cloudProductDeploy.getAccessKeyId(), cloudProductDeploy.getAccessKeySecret(), cloudDomainInfo.getStr("zoneId"), apply, cloudProductDeploy.getCertificateDomain(), configService.selectConfigByKey("system.name"));
                }else if(cloudProductDeploy.getCloudProduct().contains("cdn")){
                    aj = TxyCdnDomainUtils.putCdnDomainCert(cloudProductDeploy.getAccessKeyId(), cloudProductDeploy.getAccessKeySecret(), apply, cloudProductDeploy.getCertificateDomain(), configService.selectConfigByKey("system.name"));
                }else if(cloudProductDeploy.getCloudProduct().contains("oss")){
                    aj = TxyCdnDomainUtils.putCdnDomainCert(cloudProductDeploy.getAccessKeyId(), cloudProductDeploy.getAccessKeySecret(), apply, cloudProductDeploy.getCertificateDomain(), configService.selectConfigByKey("system.name"));
                }else{
                    aj = AjaxResult.error("未知【"+cloudProductDeploy.getCloudVendor()+"】云产品！");
                }
            }else{
                aj = AjaxResult.error("未知厂商！");
            }
            //判断是否更换成功
            if(aj.isSuccess()){
                cloudProductDeploy.setSslCreateTime(apply.getSslCreateTime());
                cloudProductDeploy.setExpiredTime(apply.getExpiredTime());
                cloudProductDeploy.setStatus("3");
                cloudProductDeploy.setDeployStatus("Y");
                //保存数据
                cloudProductDeployService.updateSslCloudProductDeploy(cloudProductDeploy);
            }
        }else{
            aj = AjaxResult.error("部署任务异常");
        }
        return aj;
    }
}
